package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsStorage;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private DtsCategoryMapper categoryMapper;

    @Override
    public IPage<DtsCategory> queryCategoryPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {


        //1. 创建分页对象
        //参数1: 当前页, 参数2: 每页显示条数
        IPage<DtsCategory> ipage = new Page<>(page,limit);


        //2. 封装查询对象
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);

        //判断查询条件不为空拼接查询条件
        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);

        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);


        }
        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //判断是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);

            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }

        }


        //3. 查询并返回结果

        IPage<DtsCategory> resultPage = categoryMapper.selectPage(ipage, queryWrapper);



        return resultPage;
    }



    @Override
    public DtsCategory add(DtsCategory category) {
        //判断制造商品牌id是否存在
        LambdaQueryWrapper<DtsCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getName,category.getName());
        Integer count = categoryMapper.selectCount(qw);
        //如果count大于0，说明此制造商id已存在
        if(count > 0) {
            return (DtsCategory) ResponseUtil.fail(-1, "此制造商ID已存在");
        }
        category.setId(category.getId());
        category.setName(category.getName());
        category.setDeleted(false);
        category.setAddTime(new Date());
        category.setIconUrl(category.getIconUrl());






        category.setKeywords(category.getKeywords());



        categoryMapper.insert(category);

        return category;


    }

    @Override
    public DtsCategory update(DtsCategory category) {
        category.setName(category.getName());
        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
        return category;
    }

    @Override
    public void deleted(DtsCategory category) {
        categoryMapper.deleteById(category.getId());
    }

    @Override
    public List<CatVo> categoryList() {
        QueryWrapper<DtsCategory> qw_p = new QueryWrapper<>();
        qw_p.eq("pid",0);
        qw_p.eq("deleted",false);

        List<DtsCategory> categories_p = categoryMapper.selectList(qw_p);

        List<CatVo> categoryList = categories_p.stream().map(category_p -> {
            CatVo catVo = new CatVo();

            catVo.setValue(category_p.getId());

            catVo.setLabel(category_p.getName());

            QueryWrapper<DtsCategory> qw_c = new QueryWrapper<>();
            qw_c.eq("pid",category_p.getId());
            qw_c.eq("deleted",false);
            List<DtsCategory> categories_c = categoryMapper.selectList(qw_c);
            List<Map<String,Object>> children = categories_c.stream().map(category_c -> {
                Map<String,Object> child = new HashMap<>();
                child.put("value",category_c.getId());
                child.put("label",category_c.getName());
                return child;
            }).collect(Collectors.toList());
            catVo.setChildren(children);

            return catVo;
        }).collect(Collectors.toList());

        return categoryList;
    }

//    @Override
//    public CategorySellVo categorySell() {
//        CategorySellVo categorySell = new CategorySellVo();
//
//        List<CategorySellAmts> categorySellData = categoryMapper.selectCategorySellData();
//        if (categorySellData==null || categorySellData.size()==0) {
//            return categorySell;
//        }
//        categorySell.setCategorySellData(categorySellData);
//
//        String[] categoryNames = new String[categorySellData.size()];
//        for (int i = 0; i < categorySellData.size(); i++) {
//            categoryNames[i] = categorySellData.get(i).getName();
//        }
//        categorySell.setCategoryNames(categoryNames);
//
//        return categorySell;
//    }

    @Override
    public Integer[] findcategoryIds(Integer defaultCategoryId) {
        List<Integer> categoryIds = new ArrayList<>();

        Integer pid = categoryMapper.selectById(defaultCategoryId).getPid();
        while (pid != 0) {
            categoryIds.add(pid);
            QueryWrapper<DtsCategory> qw = new QueryWrapper<>();
            qw.eq("id",pid);
            pid = categoryMapper.selectOne(qw).getPid();
        }
        categoryIds.add(defaultCategoryId);

        return categoryIds.toArray(new Integer[0]);
    }
}

